package me.ratel.demo.eBook.infrastructure.persistence.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;

/**
 * @author Administrator
 * @time 2016/6/14 23:18
 */
public abstract class GenericDao<T, PK extends Serializable> implements  IGenericDao<T, PK> {
    private Class<T> clazz;

    @Autowired protected SessionFactory sessionFactory;

    public GenericDao(Class<T> clazz) {
        this.clazz = clazz;
    }

    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override public T get(PK id) {
        return (T)getSession().get(clazz, id);
    }

    @Override public PK save(T entity) {
        return (PK)getSession().save(entity);
    }

    @Override public void update(T entity) {
        getSession().update(entity);
    }

    @Override public void delete(T entity) {
        getSession().delete(entity);
    }
}
